function ProductListAssistant(argFromPusher) {
	if(argFromPusher.key != undefined){
		this.isSearch = true;
	}else{
		argFromPusher.key = "";
	}
	this.title = argFromPusher.title;
	this.ancestor = argFromPusher.ancestor ? argFromPusher.ancestor : 0 ;
	this.vendor = argFromPusher.vendor ? argFromPusher.vendor : "%";
	this.recursive = argFromPusher.recursive==undefined ? true : argFromPusher.recursive;
	this.key = argFromPusher.key;
}

function getPedigree(ancestor){
	var pedigree = [ancestor];
	var menu = getStorageObject("menu");
	menu.each(function(m){
		m.parents.each(function(p) {
			if(p==ancestor){
				pedigree = pedigree.concat(getPedigree(m.tid))
			}
		});
	});	
	return pedigree;
}

ProductListAssistant.prototype = {
	ancestor: 0,
	key: "",
	vendor: "",
	recursive: true,
	searchResult: [],
	shownResult: [],
	keyupHandlerBind: null,
	title: "",
	
	setup: function() {
		Ares.setupSceneAssistant(this);
		this.makeHeader();
		if(!this.isSearch)
			this.$.resultRbt.setShowing(false);
		this.search();
	},
	
	activate: function() {
		this.$.resultRbt.setValue(this.$.resultRbt.getValue());
		this.keyupHandlerBind = function(event){
			if(event.srcElement.parentElement.id.search(/\-filterBox$/) != -1) {
				this.controller.get('filterBox').mojo.blur();
				this.filterResult();
				this.controller.get('filterBox').mojo.focus();
			}
		}.bindAsEventListener(this);
		Mojo.Event.listen(this.controller.document, "keyup", this.keyupHandlerBind);
		
		this.clickHandlerBind = function(event){
			this.$.resultRbt.setValue(this.$.resultRbt.getValue());
		}.bindAsEventListener(this);
		Mojo.Event.listen(this.controller.document, "click", this.clickHandlerBind);
	}, 
	deactivate: function() {
		Mojo.Event.stopListening(this.controller.document, "keyup", this.keyupHandlerBind);
		Mojo.Event.stopListening(this.controller.document, "click", this.clickHandlerBind);
	},
	
	makeHeader: function() {
		this.$.header.setLabel(this.title);
		if(this.title.length <= 30)
			this.controller.get("header").style["font-size"] = "16px";
		else if(this.title.length > 30 && this.title.length <= 50)
			this.controller.get("header").style["font-size"] = "12px";
		else
			this.controller.get("header").style["font-size"] = "10px";
	},
	cleanup: function() {
		Ares.cleanupSceneAssistant(this);
	},
	search: function(){
		var this1 = this;
		this.searchResult = [];
		var typeStr = "";
		if(this.recursive) 
			typeStr = getPedigree(this.ancestor).join();
		else 
			typeStr = this.ancestor;
		db.transaction( function(transaction) {
			transaction.executeSql( "SELECT distinct nid, title, type FROM products " + 
				"where title like (?) and vendor like (?) and caty in ("+ typeStr +") order by title;", 
				['%'+this1.key+'%' , this1.vendor ],
				function (transaction, result) {
					for (var i=0; i < result.rows.length; i++) {
						var row = result.rows.item(i);
						this1.searchResult.push(row);
					}
					this1.shownResult = this1.searchResult;
					this1.$.resultList.setItems(this1.shownResult);
					this1.$.panel1.setShowing(this1.searchResult.length != 0);
					this1.$.panel2.setShowing(this1.searchResult.length == 0);
				},
				errorHandler.bind(this1));
		});
	},
	resultListListtap: function(inSender, event) {
		Mojo.Controller.stageController.pushScene("productDetail",{nid: event.item.nid});
	},
	searchBtnTap: function(inSender, event) {
		this.key = this.m_key.value;
		this.search();
	},
	filterResult: function() {
		var filteredResult = this.shownResult.filter(function(element, index, array){
			return (element.title.search(new RegExp(this.m_filter.value, 'i')) !=-1);
		}.bind(this));
		this.$.resultList.setItems(filteredResult);
	},
	button2Tap: function(inSender, event) {
		Mojo.Controller.stageController.pushScene("newRequest");
	},
	resultRbtChange: function(inSender, event) {
		this.shownResult = this.searchResult.filter(function(val){
			return val.type.search(event.value)>=0 ;
		})
		this.$.resultList.setItems(this.shownResult);
		this.filterResult();
	}
};
